﻿//-----------------------------------------------------------------------
// <copyright file="ProcessingServiceElementCollection.cs" company="FastNET Services">
//     Copyright (c) 2007-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings
using System.Configuration;
#endregion

namespace FastNET.CleanAct.Client.Configuration.ProcessingServices
{
    /// <summary>
    /// ConfigurationElementCollection for Processing Services
    /// </summary>
    public class ProcessingServiceElementCollection : ConfigurationElementCollection
    {
        #region Public Indexers

        /// <summary>
        /// Gets or sets an element at the specified index
        /// </summary>
        /// <param name="index">Index of the item to work with</param>
        /// <returns>ProcessingServiceElement of the item found</returns>
        public ProcessingServiceElement this[int index]
        {
            get
            {
                return (ProcessingServiceElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        /// <summary>
        /// Gets an element at the specified index
        /// </summary>
        /// <param name="name">Name of the item to work with</param>
        /// <returns>ProcessingServiceElement of the item found</returns>
        new public ProcessingServiceElement this[string name]
        {
            get
            {
                return (ProcessingServiceElement)BaseGet(name);
            }
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the type of the <see cref="T:System.Configuration.ConfigurationElementCollection" />.
        /// </summary>
        /// <returns> The <see cref="T:System.Configuration.ConfigurationElementCollectionType" /> of this collection. </returns>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        #endregion

        #region Public Collection Management Methods

        /// <summary>
        /// Adds the specified element to the collection
        /// </summary>
        /// <param name="element">A <see cref="ProcessingServiceElement"/> to add</param>
        public void Add(ProcessingServiceElement element)
        {
            BaseAdd(element);
        }

        /// <summary>
        /// Gets the index of the specified element from the collection
        /// </summary>
        /// <param name="element">A <see cref="ProcessingServiceElement"/> to look up</param>
        /// <returns>Index of the element of -1 if the element was not found</returns>
        public int IndexOf(ProcessingServiceElement element)
        {
            return BaseIndexOf(element);
        }

        /// <summary>
        /// Removes the specified element from the collection
        /// </summary>
        /// <param name="element">A <see cref="ProcessingServiceElement"/> to remove</param>
        public void Remove(ProcessingServiceElement element)
        {
            if (BaseIndexOf(element) >= 0)
                BaseRemove(element.Name);
        }

        /// <summary>
        /// Removes the specified element by index from the collection
        /// </summary>
        /// <param name="index">The 0 based index of the element to remove</param>
        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        /// <summary>
        /// Removes the specified element from the collection
        /// </summary>
        /// <param name="name">The element name to remove</param>
        public void Remove(string name)
        {
            BaseRemove(name);
        }

        /// <summary>
        /// Clears all elements from the collection
        /// </summary>
        public void Clear()
        {
            BaseClear();
        }

        #endregion

        #region Protected Override Methods

        /// <summary>
        /// When overridden in a derived class, creates a new <see cref="T:System.Configuration.ConfigurationElement" />.
        /// </summary>
        ///<returns> A new <see cref="T:System.Configuration.ConfigurationElement" />. </returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new ProcessingServiceElement();
        }

        /// <summary>
        /// Gets the element key for a specified configuration element when overridden in a derived class.
        /// </summary>
        /// <param name="element"> The <see cref="T:System.Configuration.ConfigurationElement" /> to return the key for. </param>
        /// <returns> An <see cref="T:System.Object" /> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement" />. </returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ProcessingServiceElement)element).Name;
        }

        #endregion
    }
}
